package com.zst.bluetooth.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zst.bluetooth.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang on 2016/3/16 0016. 设备适配器
 */
public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder> {

    private Context mContext;
    /**
     * 设备列表
     */
    private List<String> devices;
    /**
     * item点击监听器
     */
    private OnItemClickListener listener;

    /**
     * 设备适配器构造器
     *
     * @param context 上下文对象
     * @param devs    设备列表
     */
    public DeviceAdapter(Context context, List<String> devs) {
        mContext = context;
        devices = devs;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_device, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String device = devices.get(position);
        //将device分割为name和addr两部分
        String[] split = device.split("##");
        String name = split[0];
        String addr = split[1];
        holder.setName(name);
        holder.setAddress(addr);
    }

    /**
     * 响应数据变化
     *
     * @param list 数据
     */
    public void notify(ArrayList<String> list) {
        devices = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null == devices ? 0 : devices.size();
    }

    /**
     * 设置item点击监听器
     *
     * @param listener 监听器
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * item点击监听器
     */
    public interface OnItemClickListener {
        /**
         * item点击
         *
         * @param position 点击的位置
         * @param addr     点击位置设备的addr
         * @param name     点击位置的设备的name
         */
        void onItemClick(int position, String addr, String name);
    }

    /**
     * 设备ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView name;
        private TextView address;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.device_name);
            address = (TextView) itemView.findViewById(R.id.device_address);
            itemView.setOnClickListener(this);
        }

        /**
         * 设置设备名称
         *
         * @param name 名称
         */
        public void setName(String name) {
            this.name.setText(name);
        }

        /**
         * 设置设备地址
         *
         * @param address 地址
         */
        public void setAddress(String address) {
            this.address.setText(address);
        }

        @Override
        public void onClick(View v) {
            String addr = devices.get(getAdapterPosition()).split("##")[1];
            String name = devices.get(getAdapterPosition()).split("##")[0];
            if (null != listener) {
                listener.onItemClick(getAdapterPosition(), addr, name);
            }
        }
    }
}
